<script>
import { getLocal,setLocal,removeLocal } from '@/utils/uni'
	export default {
		globalData: {
			latitude: '',
			longitude: ''
		},
		onLaunch: function() {
			var that = this;

			var uid = that.$us.getId();
			if (!uid)
				uid = 0;

			// that.$http.post('User/UserVisit', {userGuid:uid}, function(res) {
			// 	console.log('插入访问记录',res)
			// })

		},
		onShow: function() {
			// console.log('App Show')
			// this.expireTimeToken()
		},
		onHide: function() {
			// console.log('App Hide')
		},
		methods: {
			// 判断token是否过期
			expireTimeToken(){
				let token = getLocal('token')
				let userType = getLocal('userType')
				if(token){
					let expireTime = token.expireTime
					let nowTime = new Date().getTime()
					if(nowTime > expireTime){
						if(userType==='system'){
							removeLocal('user')
							removeLocal('token')
							uni.navigateTo({
								url: '/cmhqPage/login/login'
							})
						}else if(userType==='wechat'){
							this.loginByOpenId()
						}
					}
				}
			},
			// 通过openId登录
			loginByOpenId(){
				let _this = this
				const openId = getLocal('user').openId
				_this.$http.get('wechat/loginByOpenId', {
					openId
				}, function(res) {
					if (res.code === 'success') {
						_this.setlocalStorage(res)
					}
				})
			},
			// 存储本地token,用户信息
			setlocalStorage(result){
				let expireTime = new Date().getTime() + 3 * 1000 * 60 * 60; // 设置一小时后过期
				setLocal('token',{token: result.token,expireTime})
				setLocal('userType',result.userType)
				if(result.userType === 'wechat'){
					setLocal('user',result)
				}else if(result.userType === 'system'){
					setLocal('user',result.user)
				}
			},
		},
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-ui/index.scss";
	@import "common/demo.scss";

	page {
		font-family: PingFang SC,Microsoft Yahei,Helvetica Neue,Helvetica,Tahoma,sans-serif;
		background: #eeeeee;
	}

	.flex-start {
		display: flex;
		justify-content: flex-start;
	}

	.flex-end {
		display: flex;
		justify-content: flex-end;
	}

	.flex-center {
		display: flex;
		justify-content: center;
	}

	.flex-around {
		display: flex;
		justify-content: space-around;
	}

	.flex-between {
		display: flex;
		justify-content: space-between;
	}

	.items-center {
		align-items: center;
	}

	.mt20 {
		margin-top: 20rpx !important;
	}

	.radius20 {
		border-radius: 20rpx;
	}

	.w98 {
		width: 98%;
		margin: 0 auto;
	}

	.w96 {
		width: 96%;
		margin: 0 auto;
	}

	.w94 {
		width: 94%;
		margin: 0 auto;
	}
</style>
